package exttable.testfiller;

import dbutils.DbUtils;
import dbutils.StoredQueries;
import exttable.testfiller.formcomponents.EInputComponents;
import exttable.testfiller.formcomponents.IComponent;
import exttable.testfiller.formcomponents.InputTextComponent;
import exttable.view.ViewAnalysis;
import exttable.view.ViewInputFormComponents;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Jan 29, 2010
 * Time: 11:43:56 AM
 * To change this template use File | Settings | File Templates.
 */
public class ComponentGenerator {
    public  final String COMPONENT_TYPE = "filedType";
    public  IComponent component = new InputTextComponent();
    public static List<ViewInputFormComponents> listViewInputFormComponents;

    public List<ViewInputFormComponents> getComponent(ViewAnalysis viewAnalysis, Vector<Vector<Object>> oldData) throws Exception {
        Connection s = DbUtils.getDBConnectionGeneral();
//        System.out.println("VI " + s.isClosed());

        PreparedStatement ps = s.prepareStatement(StoredQueries.getQuery(StoredQueries.TEST_BODY));
        ps.setInt(1, viewAnalysis.getAnalysisId());
        ps.setString(2, viewAnalysis.getAnalysisVersionKey());

        ResultSet rs = ps.executeQuery();
//         System.out.println("VI " + s.isClosed());
//        System.out.println("PS r: " + ps.toString());
        List<ViewInputFormComponents> toReturn = new ArrayList<ViewInputFormComponents>();
        int i = 0;
        while (rs.next()) {
            ViewAnalysis val = new ViewAnalysis.Build()
                    .nameRu(rs.getString("name_ru"))
                    .nameEn(rs.getString("name_en"))
                    .nameKz(rs.getString("name_kk"))
                    .analysName(rs.getString("COLUMN_NAME"))
                    .isEditable(rs.getBoolean("editable")).build();
            ViewInputFormComponents vifc = new ViewInputFormComponents();
            vifc.setAnalysis(val);
            JLabel jl = new JLabel(val.toString());
            jl.setName(rs.getString(1) + "lable");
            vifc.setLabel(jl);
            vifc.setComponent(EInputComponents.valueOf(rs.getString(COMPONENT_TYPE)).getComponent(rs, oldData, null));
            if(!vifc.getAnalysis().isEditable()){
               vifc.setComponentEnable(false); 
            }
            vifc.setCheckBox(getCheckBoxWithAction(component.getCheckBox(String.valueOf(i)), vifc));
            toReturn.add(vifc);
            i++;
            listViewInputFormComponents=toReturn;
        }

        s.close();
        return toReturn;
    }

    private  JCheckBox getCheckBoxWithAction(JCheckBox checkBox, final ViewInputFormComponents component) {
        checkBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.getStateChange() != 2) {
                    component.setComponentEnable(false);
                } else {
                    component.setComponentEnable(true);
                }
            }
        });
        return checkBox;
    }
    public static void toEnableField(List<String> listSubFields){
        if(listSubFields.size()==0){
          for(ViewInputFormComponents res3:listViewInputFormComponents){
              if(!res3.getAnalysis().isEditable()){
               res3.setComponentEnable(false); 
            }
          }
        }
       for(String res:listSubFields){
          for(ViewInputFormComponents res2:listViewInputFormComponents){

             if(res.equals(res2.getAnalysis().getAnalysisName())){
//                 System.out.println("res:"+res+" res2:"+res2.getAnalysis().getAnalysisName());
                 res2.setComponentEnable(true);
             }
          }
       }

    }
}
